package com.future.mayi.app;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.multidex.MultiDexApplication;
import android.text.TextUtils;

import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.SealConst;
import com.future.mayi.activity.UserBanActivity;
import com.imnjh.imagepicker.PickerConfig;
import com.imnjh.imagepicker.SImagePicker;
import com.google.gson.Gson;
import com.future.mayi.activity.LoginActivity;
import com.future.mayi.jpush.JPushManager;
import com.future.mayi.utils.BroadcastManagerUtil;
import com.future.mayi.utils.KefuUtils;
import com.future.mayi.utils.SharedPreferencesUtils;
import com.future.mayi.R;
import com.future.mayi.utils.imageselectcache.SelectImageLoader;
import com.mob.MobSDK;
import com.pgyersdk.crash.PgyCrashManager;
import com.umeng.commonsdk.UMConfigure;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by 周大虎 on 2017/10/11.
 */

public class App extends MultiDexApplication {
    private static Gson gson;
    private static String uid = "";
    private static String userName = "";
    private static String token = "";
    private static String phone = "";
    private static String headImg = "";
    private static Context mContext;


    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        if (ApiCst.DEBUG) PgyCrashManager.register();
        SImagePicker.init(new PickerConfig.Builder().setAppContext(this)
                .setImageLoader(new SelectImageLoader())
                .setToolbaseColor(getResources().getColor(R.color.main_color)).build());
        //社会化分享
        MobSDK.init(this, "25ea9374dee49", "2e701a8ab0c7902c365660fd0067ec76");
        UMConfigure.init(mContext, UMConfigure.DEVICE_TYPE_PHONE, null);
        JPushInterface.setDebugMode(false);
        JPushInterface.init(this);

        BroadcastManagerUtil.getInstance(mContext).addAction(SealConst.EXIT, new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                App.clear();
                Intent loginActivityIntent = new Intent();
                loginActivityIntent.setClass(mContext, LoginActivity.class);
                boolean exception = false;
                if (intent != null && intent.hasExtra("exception")) {
                    exception = intent.getBooleanExtra("exception", false);
                    loginActivityIntent.putExtra("exception", exception);
                }
                if (exception) {
                    loginActivityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                } else {
                    loginActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                }
                mContext.startActivity(loginActivityIntent);
            }
        });


        BroadcastManagerUtil.getInstance(mContext).addAction(SealConst.BAN, new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                App.clear();
                Intent loginActivityIntent = new Intent();
                loginActivityIntent.setClass(mContext, UserBanActivity.class);
                loginActivityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(loginActivityIntent);
            }
        });
    }


    public static Gson getGson() {
        if (gson == null)
            gson = new Gson();
        return gson;
    }

    public static Context getmContext() {
        return mContext;
    }


    public static String getUid() {
        if (TextUtils.isEmpty(uid))
            uid = (String) SharedPreferencesUtils.get(mContext, SharedPreferencesUtils.SpEnum.LoginId, "");
        if (uid == null) setUid("");
        return uid;
    }

    public static String getUserName() {
        if (TextUtils.isEmpty(userName))
            userName = (String) SharedPreferencesUtils.get(mContext, SharedPreferencesUtils.SpEnum.LoginNickName, "");
        if (userName == null) setUid("");
        return userName;
    }

    public static String getToken(Context context) {
        Context mContext = context;
        if (mContext == null) mContext = App.getmContext();
        if (TextUtils.isEmpty(token))
            token = (String) SharedPreferencesUtils.get(mContext, SharedPreferencesUtils.SpEnum.LoginToken, "");
        if (token == null) setToken("");
        return token;
    }

    public static String getPhone() {
        if (TextUtils.isEmpty(phone))
            phone = (String) SharedPreferencesUtils.get(mContext, SharedPreferencesUtils.SpEnum.LoginPhone, "");
        if (phone == null) setPhone("");
        return phone;
    }

    public static String getHeadImg() {
        if (TextUtils.isEmpty(headImg))
            headImg = (String) SharedPreferencesUtils.get(mContext, SharedPreferencesUtils.SpEnum.LoginHeadImg, "");
        if (headImg == null) setHeadImg("");
        return headImg;
    }

    public static void setUid(String uid) {
        App.uid = uid;
    }

    public static void setToken(String token) {
        App.token = token;
    }

    public static void setPhone(String phone) {
        App.phone = phone;
    }

    public static void setHeadImg(String headImg) {
        App.headImg = headImg;
    }

    public static boolean isLogin() {
        if (TextUtils.isEmpty(getUid()))
            return false;
        else return true;
    }

    public static void clear() {
        SharedPreferencesUtils.clear(mContext, SharedPreferencesUtils.SpEnum.UserVo);
        uid = "";
        token = "";
        userName = "";
        phone = "";
        headImg = "";
        new JPushManager().setAlias("");
    }


}
